﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfellerpp
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using InterLinq.Sebarf;
using InterLinq.Server.Services.Impl;
using Sebarf.Client;
using Sebarf.Configuration;
using Sebarf.Core;
using Sebarf.Core.Interfaces;
using Sebarf.Core.Internal;
using Sebarf.ProcessChains.Interfaces;
using Sebarf.Profiles.WPF;
using Sebarf.Services.Impl;
using Sebarf.Services.Interfaces;

#endregion

namespace InterLinq.Client {
	/// <summary>
	/// is responsible for creating a interlinq Client that is using the wcf framework
	/// </summary>
	public sealed class InterLinqClientFactory {
		#region Public Methods

		public static void CreateSebarfClientForInterlinq(string RequestHandlerConfigurationFile,
														   string ServerProfileConfigurationFile,
														   string ClinetProfileConfigurationFile, string ClientId,
														   string ServiceURI, out IClient client, out ClientContext ctx,
														   out IServiceLocatorService serviceLocatorService) {
			var clientServiceLocator = new ServiceRegistrationLocatorService();
			serviceLocatorService = clientServiceLocator;
			// create engine
			client = WcfBasedClient.Create(ClientId,
											RequestHandlerEngine.Create(clientServiceLocator,
																		 RequestHandlerLoader.Load(
																			RequestHandlerConfigurationFile)), ServiceURI);

			client.Connect();

			// load profiles
			client.Process(new WpfBasedProfileLoadRequest { ProfileId = "Profile", ResourceFileName = ServerProfileConfigurationFile });
			client.ProcessLocal(new WpfBasedProfileLoadRequest { ProfileId = "Profile", ResourceFileName = ClinetProfileConfigurationFile });

			// populate remote engine
			clientServiceLocator.GetRegisteredService<IRequestRemoteHandlerEngineLocatorService>().SetRemoteEngine(client);

			var queryHandler =
				new InterLinqQueryHandler(
					new InterLinqQueryProvider(new SebarfBasedLinqQueryGateway(clientServiceLocator, client)));

			clientServiceLocator.RegisterService(new QueryHandlerLocatorService { QueryHandler = queryHandler });

			// ensure dependencies
			client.Process(new ResolveServicesDependenciesRequest());
			client.Process(new ResolveProcessChainDependenciesRequest());
			client.ProcessLocal(new ResolveServicesDependenciesRequest());
			client.ProcessLocal(new ResolveProcessChainDependenciesRequest());

			// startup services
			client.Process(new SetupServicesRequest());
			client.ProcessLocal(new SetupServicesRequest());

			ctx = new ClientContext { QueryHandler = queryHandler };
		}

		#endregion

		#region Private Methods

		private InterLinqClientFactory() {
		}

		#endregion
	}
}